package org.lsms.servlet;

/**
 * servlet used to process the paper assignment (from member to reviewer)
 * @author ZhanXueliang
 * 2010.8.31
 */

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lsms.bean.User;
import org.lsms.dao.impl.DaoImpl;
import org.lsms.mail.MailSenderInfo;
import org.lsms.mail.SimpleMailSender;

public class paper_assign2 extends HttpServlet {

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
			int reviewer_id = Integer.parseInt(request.getParameter("reviewer_id"));
			int member_id = Integer.parseInt(request.getParameter("member_id"));
			int paper_id = Integer.parseInt(request.getParameter("paper_id"));
	
			DaoImpl impl = new DaoImpl();
			
			User user = impl.get_user_info(reviewer_id);
			String loginname = user.getLoginname();
			String firstname = user.getFirstname();
			String middlename = user.getMiddlename();
			String lastname = user.getLastname();
			
			String email = user.getEmail();
			
			MailSenderInfo mailInfo = new MailSenderInfo();
			
			mailInfo.init(mailInfo);
			mailInfo.setToAddress(email);
			mailInfo.setSubject("LSMS & ICSEE 2010 Paper Assign");
			String content = "Dear Mr/Mrs " + firstname+" "+middlename+" "+lastname+'\n'+'\n';
			String letter_content = impl.get_mail_content(4);
			content +=letter_content;
			String ps = "\n\nPS:\nYour Loginname: "+loginname+".\nif you have forgotten your password, " +
					"you can click the \"forget password?\" button to reset it.";
			content +=ps;
			mailInfo.setContent(content);
			
			
			SimpleMailSender sms = new SimpleMailSender();
			
			if(impl.paper_assign2(member_id, reviewer_id, paper_id) && sms.sendTextMail(mailInfo)){
				String info = "The paper "+paper_id+" has been assigned to reviewer " +
						reviewer_id;
				response.sendRedirect("member/member_show_info.jsp?member_info="+info);
			}else{
				String info = "Problem occurs when assign papaer!Please try again later.";
				response.sendRedirect("member/member_show_info.jsp?member_info="+info);
			}
	}

	
	public void init() throws ServletException {
		
	}

}
